package com.dm.cloud.images.mode;

import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description ="docker 容器构建对象")
@Data
public class ContainerCreateMode {

    /**
     * 使用的镜像ID
     */
    @Schema(description = "镜像名称：此为必填项")
    private String image_name;

    @Schema(description = "镜像tag：此为必填项")
    private String tag_name;

    @Schema(description = "重启策略：\"\" \"no\" \"always\" \"unless-stopped\" \"on-failure\"")
    private String restartPolicy;

    /**
     * 容器名称
     */
    @Schema(description = "容器名称: 此为必填项")
    private String containerName;

    /**
     * 环境变量
     */
    @Schema(description = "环境变量")
    private Map<String, String> envGroup;

    /**
     * 端口绑定
     */
    @Schema(description = "端口绑定")
    private Map<Long, Long> portBound;

    /**
     * DNS
     */
    @Schema(description = "dns配置")
    private String dns;

    /**
     * 容器别名
     */
    @Schema(description = "容器别名")
    private String alias;

    /**
     * 主机名
     */
    @Schema(description = "主机名")
    private String hostname;

    @Schema(description = "节点ID")
    private String nodeId;

    @Schema(description = "启动命令")
    private List<String> cmd;

    @Schema(description = "数据卷")
    private Map<String,String> volumes;

    @Schema(description = "容器用户")
    private String user;

    @Schema(description = "-i  interactive 后台打印")
    private Boolean interactive;

    @Schema(description = "-t 配置后台交互 -TTY")
    private Boolean tty;

    @Schema(description = "--add-host 指定域名映射")
    private Map<String,String> extraHosts;

    @Schema(description = "--privileged 主机权限")
    private Boolean privileged;

    /**
     * 设置容器的网络模式
     *
     * “bridge”：为docker网桥上的容器创建一个新的网络堆栈
     *
     * “none”：此容器没有网络
     *
     * “container:”：重用另一个容器网络堆栈
     *
     * “host”：使用容器内的主机网络堆栈。注意：主机模式使容器能够完全访问本地系统服务，如D-bus，因此被认为是不安全的。
     *
     * 任何其他值都被解释为此容器要连接到的自定义网络的名称。
     */
    @Schema(description = "-n 网络配置，非必填: bridge，none，container，host和网络名称")
    private String network;

    @Schema(description = "关联应用ID")
    private String appCode;

    public List<String> findEnvList() {
        if (CollUtil.isEmpty(this.envGroup)) {
            return Collections.emptyList();
        }
        List<String> envs = CollUtil.newArrayList();
        for (Map.Entry<String, String> entry : this.envGroup.entrySet()) {
            envs.add(String.format("%s=%s", entry.getKey(), entry.getValue()));
        }
        return envs;
    }

}
